<?php

namespace service;

/**
 * 大淘客服务
 * Class DataokeService
 * @package service
 * @date 2019-11-10 11:33:21
 */
class DataokeService{
    const APP_KEY    = '5d5a6ce577770';
    const APP_SECRET = 'a9a4f3b09fe1d4e5fb9db4b349aa3b29';
    const API_PREFIX = 'https://openapi.dataoke.com';

    /**参数加密
     * @param $data
     * @param $appSecret
     * @return string
     */
    protected static
    function make_sign($data){
        ksort($data);
        $str = '';
        foreach($data as $k => $v){

            $str .= '&'.$k.'='.$v;
        }
        $str  = trim($str, '&');
        $sign = strtoupper(md5($str.'&key='.self::APP_SECRET));
        return $sign;
    }

    protected static
    function http_get($url, $params){

        //加密的参数
        $params['sign'] = self::make_sign($params);
        //拼接请求地址
        $url = $url.'?'.http_build_query($params);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true);
    }


    /**
     *
     *
     * totalNum: 91785,
     * pageId: "8425ca89cf91f8d9",
     * list: [ {
     * id: 23243289,
     * goodsId: "43708038994",
     * title: "【券后价99元】爱奇艺vip会员12个月官方直充视频会员不支持TV端",
     * dtitle: "【爱奇艺】vip会员12个月包年",
     * originalPrice: 199,
     * actualPrice: 99,
     * shopType: 1,
     * goldSellers: 0,
     * monthSales: 29420,
     * twoHoursSales: 549,
     * dailySales: 1276,
     * commissionType: 3,
     * desc: "爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~爱奇艺黄金会员1年，官方旗舰店，秒冲秒到账 ~",
     * couponReceiveNum: 29000,
     * couponLink: "https://uland.taobao.com/quan/detail?sellerId=1901369634&activityId=91e22d83cf444f52a3804e417cadc0d2",
     * couponEndTime: "2019-11-11 23:59:59",
     * couponStartTime: "2019-11-07 00:00:00",
     * couponPrice: 100,
     * couponConditions: "199",
     * activityType: 1,
     * createTime: "2019-11-10 09:40:03",
     * mainPic: "https://img.alicdn.com/imgextra/i2/3220506194/O1CN01E0DG8Y1vctm41Vqb6_!!3220506194.jpg",
     * marketingMainPic: "https://sr.ffquan.cn/relate_pic/o_1dovcu3f3n18a4817u016av19e4k.jpg",
     * sellerId: "1901369634",
     * cid: 7,
     * discounts: 0.5,
     * commissionRate: 20,
     * couponTotalNum: 100000,
     * haitao: 0,
     * activityStartTime: "",
     * activityEndTime: "",
     * shopName: "爱奇艺旗舰店",
     * shopLevel: 16,
     * descScore: 4.9,
     * brand: 0,
     * brandId: 134562310,
     * brandName: "爱奇艺",
     * hotPush: 439,
     * teamName: "淘起来",
     * itemLink: "https://detail.tmall.com/item.htm?id=43708038994",
     * tchaoshi: 0,
     * detailPics: "",
     * dsrScore: 4.9,
     * dsrPercent: 4.9,
     * shipScore: 4.9,
     * shipPercent: 34.17,
     * serviceScore: 4.9,
     * servicePercent: 27.78,
     * subcid: [ ],
     * tbcid: 201159608
     * }]
     * @return mixed
     */
    /** todo
     * */
    public static
    function get_goods_list(){
        //接口地址
        $host = self::API_PREFIX.'/api/goods/get-goods-list';
        //默认必传参数
        $data = [
            'appKey'   => self::APP_KEY,
            'version'  => 'v1.1.0',
            'pageSize' => 100,
            'cids'     => 1,
            //            'pageId'   => 'd920e4ad16b2b7c9'
        ];
        //加密的参数
        $data['sign'] = self::make_sign($data);
        //拼接请求地址
        $url = $host.'?'.http_build_query($data);
        //执行请求获取数据
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true);
    }

    /**
     * 高效转链 淘宝商品id
     * @param $goods_id
     */
    public static
    function get_privilege_link($goods_id){
        $url    = 'https://openapi.dataoke.com/api/tb-service/get-privilege-link';
        $params = [
            'appKey'  => self::APP_KEY,
            'version' => 'v1.1.1',
            'goodsId' => $goods_id,
        ];
        return self::http_get($url, $params);
    }

    /**
     * 超级分类
     */
    public static
    function get_super_category(){
        $url    = self::API_PREFIX.'/api/category/get-super-category';
        $params = [
            'appKey'  => self::APP_KEY,
            'version' => 'v1.1.0',
        ];
        return self::http_get($url, $params);
    }

    /**
     * 我的收藏
     */
    public static
    function get_collection($page_id = 1){
        $url    = self::API_PREFIX.'/api/goods/get-collection-list';
        $params = [
            'appKey'  => self::APP_KEY,
            'version' => 'v1.0.1',
            'pageId'  => $page_id,
        ];
        return self::http_get($url, $params);
    }

    /**
     * 我发布的商品
     */
    public static
    function get_owner_goods($page_id = 1){
        $url    = self::API_PREFIX.'/api/goods/get-owner-goods';
        $params = [
            'appKey'   => self::APP_KEY,
            'version'  => 'v1.0.1',
            'pageId'   => $page_id,
            'pageSize' => 100,
        ];
        return self::http_get($url, $params);
    }


}
